package com.woniuxy.main.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;

import com.woniuxy.main.pojo.Follow;
import com.woniuxy.main.pojo.User;
import com.woniuxy.main.provider.UserProvider;

public interface UserMapper {
	//邮箱注册
	@Insert("insert into user(umail,pwd,account,userRole) values(#{umail},#{pwd},#{account},#{userRole})")
	public int addUserByMail(User user);
	//手机注册
	@Insert("insert into user(uphone,pwd,account,userRole) values(#{uphone},#{pwd},#{account},#{userRole})")
	public int addUserByPhone(User user);
	//个性注册
	@Insert("insert into user(pwd,account,userRole) values(#{pwd},#{account},#{userRole})")
	public int addUserByAccount(User user);
	//根据账号查询用户
	@Select("select * from user where account=#{account}")
	public User selectByAccount(String account);
	//根据邮箱差用户
	@Select("select * from user where umail=#{umail}")
	public User selectByMail(String umail);
	//根据手机查用户
	@Select("select * from user where uphone=#{uphone}")
	public User selectByPhone(String uphone);
	//根据id查用户
	@Select("select * from user where uid=#{uid}")
	public User selectByUid(int uid);
	//检查账户余额
	@Select("select money from user where uid=#{uid}")
	public double balanceCheck(int uid);
	
	//更新学员个人信息
	@UpdateProvider(type=UserProvider.class,method="updateUser")
	public int addUserStuInfo(User user);

	//更新账户余额
	@Update("update user set money=#{money} where uid=#{uid}")
	public int updateMoney(User user);
	//更新用户头像保存地址
	@Update("update user set headImage=#{headImage} where uid=#{uid}")
	public void addUserHeadImage(User user);

	//更新用户信息
	@UpdateProvider(type=UserProvider.class,method="updateUser")
	public int updateUser(User user);

	//查询当前用户关注的人--关注
	@Select("select * from user where uid in (select targetUid from follow where uid = #{uid})")
	public List<User> findFollowed(int uid);
	
	//查询关注当前用户的人--粉丝
	@Select("select * from user where uid in (select uid from follow where targetUid = #{uid})")
	public List<User> findFans(int uid);
	
	//查询和当前用户相互关注的人--好友
	@Select("select * from user where uid in (select uid from follow where targetUid = #{uid} "
			+ "and uid in (select targetUid from follow where uid = #{uid}))")
	public List<User> findFriend(int uid);
	
	//取消关注
	@Delete("delete from follow where uid = #{uid} and targetUid = #{targetUid}")
	public int cancleFollow(Follow follow);
	
	//添加关注
	@Insert("insert into follow values(#{uid},#{targetUid})")
	public int addFollow(Follow follow);
	
	//校验我是否已关注ta
	@Select("select * from follow where uid = #{uid} and targetUid = #{targetUid}")
	public Follow checkFollow(Follow follow);
	
	//根据uid封禁账户，并添加封禁原因
	@Update("update user set isLock=1,lockReason=#{lockReason} where uid=#{uid}")
	public int lockAccount(Map<String,Object> request);
	
	//根据uid解除账户封禁，并移除封禁原因
	@Update("update user set isLock=0,lockReason='' where uid=#{uid}")
	public int unLockAccount(int uid);
	
	//查询手机号是否已存在
	@Select("select uphone from `user` where find_in_set(#{uphone},uphone);")
	public String isPhoneExist(String uphone);
	
	//查询邮箱是否已存在
	@Select("select umail from `user` where find_in_set(#{umail},umail);")
	public String isMailExist(String umail);
	
	//管理员添加教练账户
	@Insert("insert into user(account,address,headImage,nickName,pwd,userRole,uphone,umail) values(#{account},#{address},#{headImage},#{nickName},#{pwd},#{userRole},#{uphone},#{umail})")
	@SelectKey(keyProperty="uid",keyColumn="uid",resultType=int.class,before=false,statement="SELECT LAST_INSERT_ID()")
	public int adminAddCoach(User user);
	
	//根据uid查找用户
	@Select("select * from user where uid=#{uid}")
	public User findUserByUid(User user);
	
	//拒绝申诉请求
	@Update("update complain set isSuccess=2 where cpid=#{cpid}")
	public int refuseComplain(int cpid);
	
	//接受申诉请求
	@Update("update complain set isSuccess=1 where cpid=#{cpid}")
	public int aceptComplain(int cpid);

	//查询账号是否存在
	@Select("select umail from `user` where find_in_set(#{account},account);")
	public String isAccountExist(String account);
	
	//更改密码
	@Update("update user set pwd=#{pwd} where uid=#{uid}")
	public int changePwd(String pwd,int uid);

	//设置支付密码
	@Update("update user set payPwd=#{payPwd} where uid=#{uid}")
	public int setPwd(User user);

}
